﻿using H2.NET.Data.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace H2.NET.Web.Controls
{
    public partial class AjaxUpload : TaskControl
    {
        #region 属性 Process(文件类别)
        public string PProcess
        {
            get
            {
                return ViewState["PProcess"] == null ? "" : (string)ViewState["PProcess"];
            }
            set
            {
                ViewState["PProcess"] = value;
            }
        }
        #endregion

        #region 属性 id
        public string UploadID
        {
            get 
            {
                return ViewState["UploadID"] == null ? "file" : (string)ViewState["UploadID"]; 
            }
            set 
            {
                ViewState["UploadID"] = value;
            }
        }
        
        #endregion

        #region 属性 Step(文件类别)
        public string PStep
        {
            get
            {
                return ViewState["PStep"] == null ? "" : (string)ViewState["PStep"];
            }
            set
            {
                ViewState["PStep"] = value;
            }
        }
        #endregion

        #region 属性 PIncident(文件类别)
        public int PIncident
        {
            get
            {
                return ViewState["PIncident"] == null ? 0 : (int)ViewState["PIncident"];
            }
            set
            {
                ViewState["PIncident"] = value;
            }
        }
        #endregion 

        #region 属性 IsHaveAttachment(是否存在附件)
        public bool IsHaveAttachment
        {
            get
            {
                return ViewState["IsHaveAttachment"] == null ? false : (bool)ViewState["IsHaveAttachment"];
            }
            set
            {
                ViewState["IsHaveAttachment"] = value;
            }
        }
        #endregion

        #region 属性 AllowUpload(是否能上传)
        public bool AllowUpload
        {
            get
            {
                return ViewState["AllowUpload"] == null ? true : (bool)ViewState["AllowUpload"];
            }
            set
            {
                ViewState["AllowUpload"] = value;
            }
        }
        #endregion

        #region 属性 AllowDelete(是否能删除)
        public bool AllowDelete
        {
            get
            {
                return ViewState["AllowDelete"] == null ? true : (bool)ViewState["AllowDelete"];
            }
            set
            {
                ViewState["AllowDelete"] = value;
            }
        }
        #endregion

        #region 属性 当前用户是否上传文件
        public bool IsUpload
        {
            get
            {
                return ViewState["IsUpload"] == null ? false : (bool)ViewState["IsUpload"];
            }
            set
            {
                ViewState["IsUpload"] = value;
            }
        }
        #endregion

        public string FileClass
        {
            get
            {
                return ViewState["FileClass"] == null ? "" : (string)ViewState["FileClass"];
            }
            set
            {
                ViewState["FileClass"] = value;
            }
        }

        public List<H2.NET.Business.t_Attachment> attList = new List<Business.t_Attachment>();
        public void Page_Load(object sender, EventArgs e)
        {
            Expression exp = new Expression();
            if (string.IsNullOrEmpty(PProcess))
            {
                exp &= H2.NET.Business.t_Attachment.process == TaskAPI.Process;
                exp &= H2.NET.Business.t_Attachment.incident == TaskAPI.Incident;

                if (TaskAPI.Incident <= 0)
                {
                    exp &= H2.NET.Business.t_Attachment.username == UserAPI.UserName;
                }
            }
            else
            {
                exp &= H2.NET.Business.t_Attachment.process == PProcess;
                exp &= H2.NET.Business.t_Attachment.incident == PIncident;
            }

            exp &= H2.NET.Business.t_Attachment.fileClass == FileClass;
            exp &= H2.NET.Business.t_Attachment.status == 1;

            attList = (List<H2.NET.Business.t_Attachment>)exp.List<H2.NET.Business.t_Attachment>(Storage.Context1, new Region(0, 100), H2.NET.Business.t_Attachment.time.Asc);

            //用于区别并行流程所上传附件
            if (TaskAPI.Step == "Submit Document" || TaskAPI.Step == "Submit Margin Document")
            {
                attList = attList.Where(a => a.Step == TaskAPI.Step).ToList();
            }
            
            IsHaveAttachment = attList.Count > 0;

            IsUpload = attList.Any(t => t.UserFullName == UserAPI.UserFullName);
        }

    }
}